package flv

import (
	"bytes"
	"encoding/binary"
	"errors"
	"fmt"
	"io"
)

type Header struct {
	Version    uint8
	HasAudio   bool
	HasVideo   bool
	DataOffset uint32
}

func parseFlvHeader(header []byte) (*Header, error) {
	signature := header[0:3]
	if !bytes.Equal(signature, HeaderSignature) {
		return nil, errors.New("Signature is not matched")
	}
	version := header[3]

	flags := header[4]
	//101
	var (
		flagsAudio = false
		flagsVideo = false
	)
	if ((flags & 0x03) >> 2) == 1 {
		flagsAudio = true
	}

	if (flags & 0x01) == 1 {
		flagsVideo = true
	}

	dataOffset := binary.BigEndian.Uint32(buf[5:9])

	return &Header{
		Version:    version,
		HasAudio:   flagsAudio,
		HasVideo:   flagsVideo,
		DataOffset: dataOffset,
	}, nil
}
